package com.pcbly.web.components.email;

import java.util.HashSet;
import java.util.Set;

import javax.mail.Message;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeUtility;

import com.pcbly.web.Constants;
import com.pcbly.web.bean.mail.UserMailBean;
import com.pcbly.web.bean.mail.UserReceiveMailBean;
import com.pcbly.web.bean.mail.UserReceiveMainBean;

public class MailReceiveFrom extends ReceiveAttach {

	protected UserMailBean bean;

	protected String getFromEmail() {
		String email = "";
		if (bean.getUserName().indexOf("@") > 0) {
			return bean.getUserName();
		} else {
			email = bean.getUserName();
			if (bean.getSmtpHostName().indexOf("live.") >= 0) {
				email = email + "@hotmail.com";
			} else {
				String temp = bean.getSmtpHostName();
				temp = temp.substring(temp.indexOf(".") + 1, temp.length());
				email = email + "@" + temp;
			}
			bean.setUserName(email);
			return email;
		}
	}

	public UserMailBean getBean() {
		return bean;
	}

	public void setBean(UserMailBean bean) {
		this.bean = bean;
	}

	protected void setToOrCC(UserReceiveMainBean mainBean, Message message)
			throws Exception {
		boolean iflag = false;
		for (UserReceiveMailBean from : mainBean.getFroms()) { 
			if (getFromEmail().equals(from.getMail())) {
				iflag = true;
				break;
			}
		}
		if (iflag) {
			Set<UserReceiveMailBean> froms= new HashSet<UserReceiveMailBean>();
			for (UserReceiveMailBean from : mainBean.getFroms()) {
				from.setFlag(Constants.EMAIL_FLAG_SEND);
				froms.add(from);
			}
			mainBean.setFroms(froms);
			mainBean.setFromTos(getTo(message));
			mainBean.setFromCCs(getCc(message));
			mainBean.setFromBCCs(getBcc(message));
		} else {
			UserReceiveMailBean to = new UserReceiveMailBean();
			to.setFlag(Constants.EMAIL_FLAG_RECEIVE);
			to.setEmailFlag(Constants.EMAIL_FLAG_TO);
			to.setMail(getFromEmail());
			to.setName(bean.getUserName());
			Set<UserReceiveMailBean> tos = new HashSet<UserReceiveMailBean>(1);
			tos.add(to);
			mainBean.setFromTos(tos);
		}

	}

	protected Set<UserReceiveMailBean> getFrom(Message message)
			throws Exception {
		InternetAddress address[] = (InternetAddress[]) message.getFrom();
		if (address != null && address.length > 0) {
			return getEmail(address, Constants.EMAIL_FLAG_FROM);
		}
		return null;
	}

	protected Set<UserReceiveMailBean> getTo(Message message) throws Exception {
		InternetAddress[] address = null;
		address = (InternetAddress[]) message
				.getRecipients(Message.RecipientType.TO);

		if (address != null && address.length > 0) {
			return getEmail(address, Constants.EMAIL_FLAG_TO);
		}

		return null;
	}

	protected Set<UserReceiveMailBean> getCc(Message message) throws Exception {
		InternetAddress[] address = (InternetAddress[]) message
				.getRecipients(Message.RecipientType.CC);
		if (address != null && address.length > 0) {
			return getEmail(address, Constants.EMAIL_FLAG_CC);
		}
		return null;
	}

	protected Set<UserReceiveMailBean> getBcc(Message message) throws Exception {
		InternetAddress[] address = (InternetAddress[]) message
				.getRecipients(Message.RecipientType.BCC);

		if (address != null && address.length > 0) {
			return getEmail(address, Constants.EMAIL_FLAG_BCC);
		}
		return null;
	}

	private Set<UserReceiveMailBean> getEmail(InternetAddress[] address,
			int flag) throws Exception {
		Set<UserReceiveMailBean> list = new HashSet<UserReceiveMailBean>(
				address.length);
		for (InternetAddress add : address) {
			UserReceiveMailBean userReceiveMailBean = new UserReceiveMailBean();
			String from = add.getAddress();
			if (from == null)
				from = "";
			userReceiveMailBean.setMail(from);
			String personal = add.getPersonal();
			if (personal == null)
				personal = "";
			if (personal != "") {
				personal = MimeUtility.decodeText(personal);
			}

			if (log.isDebugEnabled()) {
				System.out.println(flag + " from::::::" + from);
				System.out.println(flag + " personal::::::" + personal);
				log.debug(flag + " from::::::" + from);
				log.debug(flag + " personal::::::" + personal);
			}
			userReceiveMailBean.setName(personal);
			userReceiveMailBean.setEmailFlag(flag);
			list.add(userReceiveMailBean);
		}
		return list;
	}
}
